package com.xiaochen.product.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochen.model.dto.h5.ProductSkuDto;
import com.xiaochen.model.entity.product.ProductSku;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import com.xiaochen.model.vo.h5.ProductItemVo;
import com.xiaochen.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    // 1.条件分页查询
    @GetMapping("/{page}/{limit}")
    public Result findPage(@PathVariable Integer page,
                           @PathVariable Integer limit,
                           ProductSkuDto productSkuDto) {
        PageInfo<ProductSku> pageInfo =productService.findByPage(page, limit,productSkuDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 2.查询商品详情
     */
    @GetMapping("/item/{skuId}")
    public Result item(@PathVariable Long skuId){
        ProductItemVo productItemVo = productService.findProductBySkuId(skuId);
        return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 远程调用:根据skuId查询sku信息
     * 注意:以前的接口返回result是为了前端获取数据方便,但是这个接口是为了我们后端远程调用,直接返回productSku即可
     * 但是,也可以返回result,只是后端取值稍微麻烦一点
     */
    @GetMapping("/getBySkuId/{skuId}")
    public ProductSku getBySkuId(@PathVariable Long skuId){
        return productService.getBySkuId(skuId);
    }
}
